package cn.gzsf.coolshark.controller;

import cn.gzsf.coolshark.entity.Product;
import cn.gzsf.coolshark.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/product/")
public class ProductController {

    @Autowired
    private ProductMapper productMapper;

    @RequestMapping("insert")
    public void insert(@RequestBody Product product){
        System.out.println("product = " + product);
        product.setViewCount(0);
        productMapper.insert(product);
    }
    @RequestMapping("select")
    public List<Product> select(){
        return productMapper.select();
    }

    @RequestMapping("delete")
    public void delete(int id){
        String url = productMapper.selectUrlById(id);
        new File("d:/files"+url).delete();
        productMapper.deleteById(id);
    }

    @RequestMapping("selectById")
    public Product selectById(int id){
        return productMapper.selectById(id);
    }

    @RequestMapping("update")
    public void update(@RequestBody Product product){
        String url = productMapper.selectUrlById(product.getId());
        new File("d:/files"+url).delete();
        productMapper.update(product);
    }

    @RequestMapping("top")
    public List<Product> top(){
        return productMapper.selectTop();
    }

    @RequestMapping("index")
    public List<Product> index(){
        return productMapper.selectIndex();
    }

    @RequestMapping("detail")
    public Product detail(int id){
        return productMapper.selectDetailById(id);
    }

    @RequestMapping("category")
    public List<Product> category(int id){
        System.out.println("id="+id);
        return productMapper.selectByCategoryId(id);
    }
}
